from datetime import datetime
from pms import db

class Department(db.Model):
    __tablename__ = 'department'

    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(50),unique=True)

    def __init__(self,name):
        self.name = name

    def __repr__(self):
        return '<部门 {} ：{}>'.format(self.id,self.name)

    def to_json(self):
        '''为转json格式定义的字典方法'''
        return dict(id=self.id,name=self.name)


class Employee(db.Model):
    '''员工表'''
    __tablename__ ='employee'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(50),nullable=False)
    gender = db.Column(db.String)
    job = db.Column(db.String)
    birthdate = db.Column(db.Date)
    idcard = db.Column(db.String)
    address = db.Column(db.String)
    salary = db.Column(db.Float)
    release_time = db.Column(db.DateTime,default=datetime.now())

    department_id = db.Column(db.Integer,db.ForeignKey('department.id'))
    department = db.relationship('Department',backref=db.backref('employees',lazy='dynamic'))

    def to_json(self):
        return dict(id=self.id,name=self.name,gender=self.gender,job=self.job,birthdate=self.birthdate,idcard=self.idcard,
                    address=self.address,salary=self.salary)

    def __init__(self,name=None,gender=None,job=None,birthdate=None,idcard=None,address=None,salary=None,release_time=None):
        self.name = name
        self.gender = gender
        self.job = job
        self.birthdate = birthdate
        self.idcard = idcard
        self.address=address
        self.salary=salary
        self.release_time=release_time if release_time else datetime.now()

    def __repr__(self):
        return '<员工 {}:{} {} {}>'.format(self.id,self.name,self.salary,self.address)